package com.mallbook.controller.servlet.admin;

import com.mallbook.model.exception.ServiceException;
import com.mallbook.model.pojo.Orders;
import com.mallbook.model.pojo.PageBean;
import com.mallbook.model.pojo.User;
import com.mallbook.model.service.OrderService;
import com.mallbook.model.utils.MallDefaultConfig;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-07-06
 */
@WebServlet("/admin/order")
public class OrderServlet extends HttpServlet {
    private OrderService orderService = new OrderService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opr = req.getParameter("opr");
        switch (opr) {
            case "new":
                doNew(req, resp);
                break;
            case "all":
                doAll(req, resp);
                break;
            case "del":
                doDel(req, resp);
                break;

        }
    }

    private void doDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        JSONObject jsonObject = new JSONObject();
        PrintWriter writer = resp.getWriter();
        try {
            orderService.delOrder(null, Integer.parseInt(oid));
            jsonObject.put("msg", "ok");
        } catch (ServiceException e) {
            e.printStackTrace();
            jsonObject.put("msg", "系统繁忙，请稍后再试");
        }
        writer.print(jsonObject);
        writer.close();
    }

    /**
     * 查询所有订单信息
     *
     * @param req
     * @param resp
     */
    private void doAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String p = req.getParameter("p");
        String pSize = req.getParameter("pSize");
        String state = req.getParameter("state");
        Orders orders = new Orders();
        if (state != null && !state.isEmpty()) {
            orders.setState(Integer.parseInt(state));
        }

        String name = req.getParameter("name");
        if (name != null && !name.isEmpty()) {
            orders.setConSingnee(name);
        }
        String sTime = req.getParameter("sTime");
        if (sTime != null && !sTime.isEmpty()) {
            orders.setsTime(sTime);
        }
        String eTime = req.getParameter("eTime");
        if (eTime != null && !eTime.isEmpty()) {
            orders.seteTime(eTime);
        }

        if (pSize != null && !pSize.equals("")) {
            MallDefaultConfig.ORDER_PAGE_SIZE = Integer.parseInt(pSize);
        } else {
            MallDefaultConfig.ORDER_PAGE_SIZE = 5;
        }
        PageBean<Orders> ordersPageBean =
                orderService.getOrdersByCondition(orders, null, Integer.parseInt(p));
        req.setAttribute("pageBean", ordersPageBean);
        req.setAttribute("order", orders);
        req.getRequestDispatcher("/WEB-INF/admin/order_list.jsp").forward(req, resp);
    }

    /**
     * 查询最新十条订单数据
     *
     * @param req
     * @param resp
     */
    private void doNew(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Orders> ordersList = orderService.getNewOrders(10);
        req.setAttribute("ordersList", ordersList);
        req.getRequestDispatcher("/WEB-INF/admin/index.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
